package com.tduck.cloud.poster.controller;

import java.util.List;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tduck.cloud.common.util.QueryWrapperUtils;
import com.tduck.cloud.common.util.Result;
import com.tduck.cloud.common.util.SecurityUtils;
import com.tduck.cloud.common.util.ShortIdUtils;
import com.tduck.cloud.poster.entity.PosterEntity;
import com.tduck.cloud.poster.service.PosterService;
import com.tduck.cloud.poster.util.ImageCombinerBuilder;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tduck.cloud.poster.entity.PosterTemplateEntity;
import com.tduck.cloud.poster.service.PosterTemplateService;

/**
 * 海报模板
 * @author tduck-gen
 * @since  2023-02-14 17:22:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/poster/template")
public class PosterTemplateController {
    private final PosterTemplateService posterTemplateService;
    private final PosterService posterService;

    /**
     * 查询海报模板列表
     */
    @GetMapping("/list")
    public Result<List<PosterTemplateEntity>> list(PosterTemplateEntity posterTemplate) {
        LambdaQueryWrapper<PosterTemplateEntity> queryWrapper = Wrappers.<PosterTemplateEntity>lambdaQuery().isNull(PosterTemplateEntity::getUserId)
                .or().eq(PosterTemplateEntity::getUserId, SecurityUtils.getUserId());
        return Result.success(posterTemplateService.list(queryWrapper));
    }


    /**
     * 保存为我的模板
     */
    @SneakyThrows
    @PostMapping("/save")
    public Result<Boolean> savePosterAsTemplate(@RequestBody PosterEntity posterEntity) {
        PosterTemplateEntity posterTemplate = new PosterTemplateEntity();
        posterTemplate.setUserId(SecurityUtils.getUserId());
        posterEntity.setId(null);
        posterTemplate.setPosterTemplateConfig(posterEntity);
        posterTemplate.setName(posterEntity.getName());
        posterTemplate.setPosterKey(ShortIdUtils.genId());
        // 生成一张图片保存
        posterTemplate.setTemplateImage(ImageCombinerBuilder.uploadImageUrl(posterEntity.getPosterKey(), ImageCombinerBuilder.build(posterEntity).getCombinedImageStream()));
        return Result.success(posterTemplateService.save(posterTemplate));
    }

    /**
     * 删除我的模板
     */
    @PostMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        LambdaQueryWrapper<PosterTemplateEntity> queryWrapper = Wrappers.<PosterTemplateEntity>lambdaQuery()
                .eq(PosterTemplateEntity::getId, id)
                .eq(PosterTemplateEntity::getUserId, SecurityUtils.getUserId());
        return Result.success(posterTemplateService.remove(queryWrapper));
    }


    /**
     * 根据模板创建海报
     */
    @SneakyThrows
    @PostMapping("/useTemplateToPoster")
    public Result<PosterEntity> useTemplateToPoster(@RequestBody PosterEntity posterReq) {
        PosterTemplateEntity posterTemplate = posterTemplateService.getById(posterReq.getId());
        if (ObjectUtil.isNull(posterTemplate) || !posterTemplate.getUserId().equals(SecurityUtils.getUserId())) {
            return Result.failed("模板不存在");
        }
        PosterEntity posterEntity = posterTemplate.getPosterTemplateConfig();
        PosterEntity updatePoster = posterService.getOne(Wrappers.<PosterEntity>lambdaQuery().eq(PosterEntity::getPosterKey, posterReq.getPosterKey()));
        if(ObjectUtil.isNull(updatePoster)){
            return Result.failed("海报不存在");
        }
        updatePoster.setPosterConfig(posterEntity.getPosterConfig());
        updatePoster.setPosterWidgetList(posterEntity.getPosterWidgetList());
        posterService.updateById(updatePoster);
        return Result.success(updatePoster);
    }


}
